package net.bolue.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter{
	
	protected String encoding = null;
	protected FilterConfig filterConfig = null;
	protected boolean ignore = true;

	public void destroy() {
		
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		
		if(this.ignore||(servletRequest.getCharacterEncoding() == null)){
			servletRequest.setCharacterEncoding(this.encoding);
		}
		filterChain.doFilter(servletRequest, servletResponse);
	}

	public void init(FilterConfig paramFilterConfig) throws ServletException {
		this.filterConfig = paramFilterConfig;
		this.encoding = paramFilterConfig.getInitParameter("encoding");
		String str = paramFilterConfig.getInitParameter("ignore");
		if (str == null) {
			this.ignore = true;
		}else if (str.equalsIgnoreCase("true")) {
			this.ignore = true;
		}else if (str.equalsIgnoreCase("yes")) {
			this.ignore = true;
		}else{
			this.ignore = false;
		}
	}

}
